@echo off
echo ========================================
echo        MQTT ngrok Tunnel Starter
echo ========================================
echo.

REM Check if ngrok.exe exists
if not exist "ngrok.exe" (
    echo [ERROR] ngrok.exe not found!
    echo Please ensure ngrok.exe is in the current directory.
    pause
    exit /b 1
)

:menu
echo Please select tunnel type to start:
echo.
echo 1. Backend tunnel (port 3002)
echo 2. Frontend tunnel (port 5173)
echo 3. Start both tunnels
echo 4. Custom port
echo 5. Exit
echo.

set /p choice=Enter your choice (1-5): 

if "%choice%"=="1" goto backend
if "%choice%"=="2" goto frontend
if "%choice%"=="3" goto both
if "%choice%"=="4" goto custom
if "%choice%"=="5" goto exit
echo [ERROR] Invalid choice, please enter 1-5
echo.
goto menu

:backend
echo.
echo Stopping existing tunnels first...
taskkill /f /im ngrok.exe >nul 2>&1
timeout /t 2 /nobreak >nul
echo Starting backend tunnel (port 3002)...
start "ngrok-backend" cmd /k "ngrok.exe http 3002"
echo Backend tunnel started!
echo Visit http://localhost:4040 to view tunnel address
goto end

:frontend
echo.
echo Stopping existing tunnels first...
taskkill /f /im ngrok.exe >nul 2>&1
timeout /t 2 /nobreak >nul
echo Starting frontend tunnel (port 5173)...
start "ngrok-frontend" cmd /k "ngrok.exe http 5173"
echo Frontend tunnel started!
echo Visit http://localhost:4040 to view tunnel address
goto end

:both
echo.
echo Stopping existing tunnels first...
taskkill /f /im ngrok.exe >nul 2>&1
timeout /t 2 /nobreak >nul
echo Starting both tunnels...
start "ngrok-backend" cmd /k "ngrok.exe http 3002"
timeout /t 2 /nobreak >nul
start "ngrok-frontend" cmd /k "ngrok.exe http 5173"
echo Both tunnels started!
echo Visit http://localhost:4040 to view tunnel addresses
goto end

:custom
echo.
set /p port=Enter port number: 
if "%port%"=="" (
    echo Port number cannot be empty!
    goto custom
)
echo.
echo Starting custom tunnel (port %port%)...
start "ngrok-custom" cmd /k "ngrok.exe http %port%"
echo Custom tunnel started!
echo Visit http://localhost:4040 to view tunnel address
goto end

:exit
echo.
echo Exiting program
goto end

:end
echo.
echo ========================================
echo Usage:
echo - Tunnel addresses will be shown in new windows
echo - Visit http://localhost:4040 to view addresses
echo - Run get-tunnel-urls.cmd to get addresses
echo - Run stop-tunnels.cmd to stop all tunnels
echo ========================================
echo.
pause